import {getCartList} from "api/api.js"
export default {
  state:{
    cartInfoList:[]
  },
  getters:{
    checkedAll(state){
      let count = state.cartInfoList.reduce((pre,item)=>item.isChecked===1?pre+1:pre,0)
      return count===state.cartInfoList.length && state.cartInfoList.length !== 0
    },
    select(state){
      let arr = []
      state.cartInfoList.forEach(item=>{
        if(item.isChecked===1)
          arr.push(item.skuId)
      })
      return arr
    },
    notSelect(state){
      let arr = []
      state.cartInfoList.forEach(item=>{
        if(item.isChecked===0)
          arr.push(item.skuId)
      })
      return arr
    }
  },
  mutations:{
    getCartList(state,cartInfoList){
      state.cartInfoList=cartInfoList
    }
  },
  actions:{
    async getCartList(store){
      try{
        const {data:[info],code} = await getCartList()
        if(code===200){
          let cartInfoList = []
          if(info){
            cartInfoList = info.cartInfoList
          }
          store.commit("getCartList",cartInfoList)
        }else{

        }
        return {info,code}
      }catch(err){
        return Promise.reject(err)
      }
    }
  }
}